﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;

/// <summary>
/// ShortTextInput is used for obtaining a single line of text.
/// This text is accessible via Text property.
/// Also offers validation group and Required property which causes validation.
/// </summary>
public partial class ShortTextInput : ScrumInput
{

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this.RequiredFieldValidatorInput.ErrorMessage = String.Format((string)GetLocalResourceObject("RequiredFieldError"), FieldLabel.Text);
        
    }

    /// <summary>
    /// Handles the Pre render event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (this.IsNumeric)
        {
            this.ValidationExpression = "^[0-9]+$";
            this.ErrorMessage = "Please enter a number here";
        }
    }

    /// <summary>
    /// Gets or sets the maximum of characters user can enter into this textbox.
    /// </summary>
    /// <value>The length of the max.</value>
    public int MaxLength 
    {
        get 
        {
            return this.FieldInput.MaxLength;
        }
        set
        {
            this.FieldInput.MaxLength = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is numeric - allow user only to enter number into textbox
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is numeric; otherwise, <c>false</c>.
    /// </value>
    public bool IsNumeric 
    { 
        get; 
        set; 
    }

    #region [Validation]

    /// <summary>
    /// Gets or sets the validation group for input
    /// </summary>
    /// <value>The validation group.</value>
    public override string ValidationGroup
    {
        get
        {
            return validationGroup;
        }
        set
        {
            validationGroup = value;
            this.RequiredFieldValidatorInput.ValidationGroup = validationGroup;
            this.RegularExpressionValidatorInput.ValidationGroup = validationGroup;
            this.FieldInput.ValidationGroup = validationGroup;
        }
    }
    
    private bool required;

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="ShortTextInput"/> is required.
    /// </summary>
    /// <value><c>true</c> if required; otherwise, <c>false</c>.</value>
    public bool Required
    {
        get
        {
            return this.required;
        }
        set{
            this.RequiredFieldValidatorInput.ControlToValidate = value? this.FieldInput.ID : this.FakeTextBox.ID;
            required = value;
        }
    }

    /// <summary>
    /// Gets or sets the error message.
    /// </summary>
    /// <value>The error message.</value>
    public string ErrorMessage 
    { 
        get { return this.RequiredFieldValidatorInput.ErrorMessage; }
        set { this.RequiredFieldValidatorInput.ErrorMessage = value; }
    }

    /// <summary>
    /// Gets or sets the validation expression (regexp) against which is control validated.
    /// ErrorMessageExpression must be set when this property set to work correctly
    /// </summary>
    /// <value>The validation expression.</value>
    public string ValidationExpression
    {
        get
        {
            return this.RegularExpressionValidatorInput.ValidationExpression;
        }
        set
        {
            this.RegularExpressionValidatorInput.ControlToValidate = this.FieldInput.ID;
            RegularExpressionValidatorInput.ValidationExpression = value;
        }
    }

    /// <summary>
    /// Gets or sets the error message for case when text in input is not matching regexp ValidationExpression
    /// </summary>
    /// <value>The error message expression.</value>
    public string ErrorMessageExpression
    {
        get {return this.RegularExpressionValidatorInput.ErrorMessage;}
        set { this.RegularExpressionValidatorInput.ErrorMessage = value; }
    }

    ///// <summary>
    ///// Validates this instance.
    ///// </summary>
    ///// <returns></returns>
    //public void Validate()
    //{
    //    this.RequiredFieldValidatorInput.Validate();
    //    this.RegularExpressionValidatorInput.Validate();
    //    if (!this.IsValid) FieldLabel.CssClass = CSSLabelInError;
    //}

    ///// <summary>
    ///// Gets a value indicating whether this instance is valid.
    ///// </summary>
    ///// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
    //public bool IsValid
    //{
    //    get
    //    {
    //        return RegularExpressionValidatorInput.IsValid && RequiredFieldValidatorInput.IsValid;
    //    }
    //}

    #endregion

    protected override Label GetLabel()
    {
        return this.FieldLabel;
    }

    protected override WebControl GetWebControl()
    {
        return this.FieldInput;
    }

}
